package com.shanzmoo.admin.web.store;

import com.shanzmoo.admin.domain.dto.StoreStockUpdateDTO;
import com.shanzmoo.core.manager.store.StoreStockManager;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.db.store.entity.StoreStockCheckBillDetailEntity;
import com.shanzmoo.db.store.entity.query.StoreStockQuery;
import com.shanzmoo.db.store.entity.vo.StoreStockVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存实时明细 控制层
 *
 * @author fs 2021-03-09
 */
@Api(tags = "库存实时明细")
@RestController
@RequestMapping("/store/stock")
public class StoreStockController {

    @Autowired
    private StoreStockManager storeStockManager;

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<StoreStockVO>> pageList(@Valid @RequestBody StoreStockQuery query) {
        PageRespBase<StoreStockVO> pageList = storeStockManager.queryPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "设置预警数量")
    @PostMapping("/setAlarm")
    public SHttpResponse<Boolean> setAlarm(@Valid @RequestBody StoreStockUpdateDTO updateDTO) {
        storeStockManager.setAlarm(updateDTO.convertEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "创建盘点单")
    @PostMapping("/createCheckStock")
    public SHttpResponse<Boolean> createCheckStock(@Valid @RequestBody List<StoreStockVO> tmps) {
        List<StoreStockCheckBillDetailEntity> list = new ArrayList<>();
        for(StoreStockVO stock:tmps){
            StoreStockCheckBillDetailEntity detail = new StoreStockCheckBillDetailEntity();
            detail.setSku(stock.getSku());
            detail.setStoreCode(stock.getStoreCode());
            detail.setSysSkuNum(stock.getSkuNum());
            detail.setActualSkuNum(0);
            list.add(detail);
        }
        storeStockManager.createCheckStock(list);
        return SHttpResponse.ok(Boolean.TRUE);
    }
 }
